home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / dynCreateSoft.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  11.3 KB  |  421 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Description:
  22. //      This script is invoked from the Soft/Rigid body menu to make soft bodies.
  23. //  See the main procedure ("dynCreateSoft") for arguments and description.
  24. //
  25. //  Return Value:
  26. //      None.
  27. //
  28.  
  29. proc int objInList( string $obj, string $list[] )
  30. //
  31. // returns true if $obj is in $list, false otherwise
  32. // This routine does not consider parents, etc.  Considers shape and its transform to be different.
  33. {
  34.     int $i = 0;
  35.     for ($i = 0; $i < size($list); $i++)
  36.         if ($obj == $list[$i]) return 1;
  37.  
  38.     return 0;
  39. }
  40.  
  41. proc int parentInList( string $obj, string $list[] )
  42. //
  43. // returns true if any parent of $obj is in $list, false otherwise
  44. {
  45.     string $parents[] = `listRelatives -parent $obj`;
  46.  
  47.     int $i = 0;
  48.     for ($i = 0; $i < size($parents); $i++)
  49.     {
  50.         if (objInList( $parents[$i], $list )) 
  51.         {
  52.             return 1;
  53.         }
  54.     }
  55.  
  56.     return 0;
  57. }
  58.  
  59. proc int childInList( string $obj, string $list[] )
  60. //
  61. // returns true if any child of $obj is in $list, false otherwise
  62. {
  63.     string $children[] = `listRelatives $obj`;
  64.  
  65.     int $i = 0;
  66.     for ($i = 0; $i < size($children); $i++)
  67.     {
  68.         if (objInList( $children[$i], $list )) 
  69.         {
  70.             return 1;
  71.         }
  72.     }
  73.  
  74.     return 0;    
  75. }
  76.  
  77. proc string canBeSoft( string $obj )
  78. //
  79. // Description:
  80. //    If $obj is a node which can itself be made soft, 
  81. // or is the parent transform of such a node, or the parent of a hierarchy 
  82. // which contains exactly one such node,
  83. // returns the name of the unique valid node.
  84. // Otherwise returns an empty string.
  85. {
  86.     string $result = "";
  87.     string $ntype = `nodeType $obj`;
  88.  
  89.     if (($ntype == "nurbsSurface") ||
  90.        ($ntype == "nurbsCurve") ||
  91.        ($ntype == "mesh") ||
  92.        ($ntype == "lattice"))
  93.     {
  94.         $result = $obj;
  95.     }
  96.     else
  97.     if ($ntype == "transform")
  98.     {
  99.         int $successCount = 0;
  100.         string $validObj = "";
  101.  
  102.         // We have to be a little careful here.  With a deformer,
  103.         // listRelatives will give us back both the current and 
  104.         // shapes. So we want only the current shape.
  105.         // But we also want to include all the children which are
  106.         // transforms so that we want down hierarchies.
  107.         // To do this, first do listRelatives -s and check the
  108.         // first shape only.  Then do a full listRelatives and
  109.         // recurse only on the children that are transforms.
  110.         // 
  111.         string $kidShapes[] = `listRelatives -fullPath -s $obj`;
  112.         if (size($kidShapes) > 0)
  113.         {
  114.             $validObj = canBeSoft( $kidShapes[0] );
  115.             if (size($validObj) > 0)
  116.             {
  117.                 $successCount++;
  118.                 $result = $validObj;
  119.             }
  120.         }
  121.  
  122.         // Now do child transforms.
  123.         //
  124.         string $kids[] = `listRelatives -fullPath $obj`;
  125.         int $i;
  126.         for ($i = 0; $i < size($kids); $i++)
  127.         {
  128.             // If we got a valid object here and also above, that
  129.             // is a failure condition anyway and we will return an
  130.             // empty string.  So it doesn't matter if we overwrite
  131.             // the value here.
  132.             //
  133.             if (nodeType($kids[$i]) == "transform")
  134.             {
  135.                 $validObj = canBeSoft( $kids[$i] );
  136.                 if (size($validObj) > 0)
  137.                 {
  138.                     $successCount++;
  139.                     $result = $validObj;
  140.                 }
  141.             }
  142.         }
  143.  
  144.         if ($successCount > 1)
  145.         {
  146.             $result = "";
  147.         }
  148.     }
  149.  
  150.     return $result;
  151. }
  152. global proc dynCreateSoft( int $hide, int $history, int $goal, float $goalWeight, int $makeCopySoft ) 
  153. //
  154. // Description:
  155. //    Implement the "duplicate, make copy soft" and "duplicate, mnake original soft"
  156. // options of the soft body menu.
  157. // If $hide is 1, hide the original object;
  158. // If $history is 1, duplicate the upstream graph ("duplicate -un")
  159. // If $goal is 1, make the non-soft object a goal using weight $goalWeight. 
  160. {
  161.     // Get selected objects.  Need to save them in case we wish to hide
  162.     // them or use them as goals.
  163.     //
  164.     string $selList[] = `ls -sl`;
  165.     string $objList[];
  166.     string $rigidNodes[] = `ls -type rigidBody`;
  167.  
  168.     // Build the actual list we will work on.
  169.     // Screen out any objects whose parents
  170.     // are already in list, or which are already soft.
  171.     //
  172.     int $i;
  173.     int $objIndex = 0;
  174.     for (    $i = 0; $i < size( $selList ); $i++ )
  175.     {
  176.         string $obj = $selList[ $i ];
  177.  
  178.         // Is this already a particle object?
  179.         //
  180.         if (`particleExists $selList[$i]`)
  181.         {
  182.             error( $obj  + " is already a  particle object or soft body. Please de-select it.");    
  183.         }
  184.         else
  185.         //    
  186.         // is this a rigid node or the parent of one?
  187.         //    
  188.         if ((!$makeCopySoft) && ((objInList( $obj, $rigidNodes )) ||
  189.                                   (childInList( $obj, $rigidNodes ))))
  190.         {
  191.             error( $obj + " belongs to a rigid node. It cannot be converted into a soft body. Please select the original geometry shape and use the duplicate option. " );
  192.         }
  193.         else
  194.         {
  195.             // Get the actual valid geometry is there is one.
  196.             //
  197.             string $validObj = canBeSoft($obj);
  198.             if (0 == size($validObj))
  199.             {
  200.                 error( $obj + ": This object cannot be made soft (it has no valid geometry, or too many). Please check your selection." );
  201.             }
  202.             else
  203.             //
  204.             // is the object in the list already?
  205.             //
  206.             if ( (!parentInList( $validObj, $objList )) && 
  207.                       (!objInList( $validObj, $objList )) )
  208.             {
  209.                 // Add to list.
  210.                 //
  211.                 $objList[ $objIndex ] = $validObj;
  212.                 $objIndex++;
  213.             }
  214.         }
  215.     }
  216.  
  217.     // do we have any valid objects to work on?
  218.     //
  219.     if (size($objList) == 0)
  220.     {
  221.         error( "no objects which could be made soft were selected. Please select a NURBS, polygonal,  wire, or  lattice object which isn't already a rigid or soft body.");
  222.     }
  223.  
  224.     // At this point the pared-down list contains only
  225.     // objects which can be made soft, so we can proceed to work on them.
  226.     // Replace original selection list with pared-down list
  227.     // This is ok because what we are about to do will change the selection anyway.
  228.     //
  229.     string $dupObj[];
  230.  
  231.     int $index = 0;
  232.     for ($index = 0; $index < size($objList); $index++)
  233.     {
  234.         // Duplicate the selected objects.
  235.         // dupObj stores the return from "duplicate."
  236.         // Note that this is always the transform of the duplicated object.
  237.         // Duplicate individually and store only the fifrst item in the result.
  238.         // This is necessary because when there is a curve on surface,
  239.         // the names of both the surface and the curve are returned from
  240.         // duplicate, and we do not want the latter.
  241.         // 
  242.         string $result[];
  243.         select -r $objList[$index];
  244.         if ($history)
  245.             $result = `duplicate -un`;
  246.         else
  247.             $result = `duplicate`;    
  248.  
  249.         $dupObj[$index] = $result[0];
  250.     }
  251.  
  252.     string $softObj[];
  253.     if ($makeCopySoft)
  254.     {
  255.         // make duplicate soft:
  256.         // Remove any duplicate rigid bodies just created.
  257.         // The duplicate operation duplicates all the children
  258.         // including the rigid body.   We do not want the duplicate
  259.         // rigid body because then we cannot make it soft.
  260.         // Note that we can get in this situation only if the user
  261.         // selected the original geometry of the rigid body shape;
  262.         // that is a correct workflow and we want to allow it.
  263.         //
  264.         string $dupKids[] = `listRelatives -pa $dupObj`;
  265.         for ($i = 0; $i < size($dupKids); $i++)
  266.         {
  267.             if ("rigidBody" == `nodeType $dupKids[$i]`) 
  268.             {
  269.                 delete $dupKids[$i];
  270.             }
  271.         }
  272.  
  273.         // Now parent copy to world, if it's not already
  274.         //
  275.         for ($i = 0; $i < size($dupObj); $i++)
  276.         {
  277.             string $theDupObj = $dupObj[$i];
  278.             if (size(`listRelatives -parent $theDupObj`) > 0)
  279.                 parent -world $dupObj[$i];
  280.         }
  281.  
  282.         // Rename the copy according to naming convention established.
  283.         // Store the new name of the copy.
  284.         //
  285.         for ($i = 0; $i < size($dupObj); $i++)
  286.         {
  287.             if (size($objList[$i]) > 0)
  288.             {
  289.                 string $parentName[] = `listRelatives -parent $objList[$i]`;
  290.                 string $newName = "copyOf"+ $parentName[0];
  291.                 $dupObj[$i] = `rename $dupObj[$i] $newName`;
  292.             }
  293.         }    
  294.  
  295.         // make copy soft
  296.         //
  297.         select -r $dupObj;
  298.         $softObj = `soft -c`;
  299.     }
  300.     else
  301.     {
  302.         // make original soft:
  303.         // if duplicate list has any rigid bodies,
  304.         // then we can't do this.  Delete the duplicates and exit.
  305.         //
  306.         string $dupKids[] = `listRelatives -pa $dupObj`;
  307.         for ($i = 0; $i < size($dupKids); $i++)
  308.         {
  309.             if ("rigidBody" == `nodeType $dupKids[$i]`) 
  310.             {
  311.                 // suppress cycle check so it doesn't hide our warning
  312.                 //
  313.                 cycleCheck -e off;
  314.  
  315.                 // Warn the user and delete unwanted duplicates
  316.                 //
  317.                 delete $dupObj;
  318.                 error("Cannot make original soft with rigid body. Please choose the 'make copy soft' option.");
  319.             }
  320.         }
  321.  
  322.         // Parent original to world, if it's not already
  323.         //
  324.         string $copyOfName[];
  325.         clear( $copyOfName );
  326.         for ($i = 0; $i < size($objList); $i++)
  327.         {
  328.             if (size($objList[$i]) > 0)
  329.             {
  330.                 string $objParent[] = `listRelatives -parent $objList[$i]`;
  331.                 if (size(`listRelatives -parent $objParent[0]`) > 0)
  332.                 {
  333.                     parent -world $objParent[0];
  334.                     string $selected[] = `ls -sl`;
  335.                     $copyOfName[$i] = $selected[0];
  336.                     $objList[$i] = $selected[0];
  337.                 }
  338.                 else
  339.                 {
  340.                     string $parentName[] = `listRelatives -parent $objList[$i]`;
  341.                     $copyOfName[$i] = $parentName[0];
  342.                 }
  343.             }
  344.         }
  345.  
  346.         // Rename copies according to established naming convetion
  347.         // Store the new name of the copy.
  348.         //
  349.         for ($i = 0; $i < size($dupObj); $i++)
  350.         {
  351.             if (size($objList[$i]) > 0)
  352.             {
  353.                 string $newName =  ("copyOf"+ $copyOfName[$i]);
  354.                 $dupObj[$i] = `rename $dupObj[$i] $newName`;
  355.             }
  356.         }    
  357.  
  358.         // Make original soft
  359.         //
  360.         select -r $objList;
  361.         $softObj = `soft -c`;
  362.     }
  363.  
  364.     int $objCount = size( $objList );
  365.     if ($objCount != size($softObj))
  366.     {
  367.         warning("Soft body command failed. You may have unwanted duplicate objects. Please delete them.");
  368.         return;
  369.     }
  370.  
  371.     // If everything got made soft; set up the goals.
  372.     // Otherwise, issue a warning.
  373.     //
  374.     if ($goal)
  375.     {
  376.         if ($makeCopySoft)
  377.         {
  378.             // Make the original objects goals for the soft objects.
  379.             // Note: we checked above that objList and softObj are same length
  380.             //
  381.             for ($i = 0; $i < $objCount; $i++)
  382.             {
  383.                 goal -g $objList[$i] -w $goalWeight $softObj[$i];
  384.             }
  385.         }
  386.         else
  387.         {
  388.             // Make the duplicate objects goals for the soft objects.
  389.             // Note: we checked above that objList and softObj are same length
  390.             //
  391.             for ($i = 0; $i < $objCount; $i++)
  392.             {
  393.                 goal -g $dupObj[$i] -w $goalWeight $softObj[$i];
  394.             }
  395.         }
  396.     }
  397.  
  398.     // If hide was thrown, hide the non-soft objects.
  399.     //
  400.     if ($hide)
  401.     {
  402.         if ($makeCopySoft)
  403.         {
  404.             hide $objList;    
  405.         }
  406.         else
  407.         {
  408.             hide $dupObj;
  409.         }
  410.     }
  411.  
  412.  
  413.     // Select parent transforms of new particle objects.
  414.     //
  415.     select -cl;
  416.     for( $i = 0; $i < size( $softObj ); $i++)
  417.     {
  418.         select -tgl `listRelatives -parent $softObj[$i]`;
  419.     }
  420. }
  421.